package com.x.example.page;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

/**
 * 可以根据进度-设置文字选中颜色的单行textView
 */
public class TextProgressView extends View {
    public interface IProgressDrawable {
        void setProgress(float progress);
    }

    private Paint mPaint;
    private float mTextWidth;
    private float mTextHeight;
    private String mText;
    private int mNormalTextColor;
    private int mSelectedTextColor;
    private float mProgress;
    private RectF mProgressRectF = new RectF();
    private Bitmap mShadowBitmap = null;

    public TextProgressView(Context context) {
        super(context);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setXfermode(null);
        mPaint.setTextAlign(Paint.Align.LEFT);
    }

    public void setTextSize(int unit, float value) {
        mPaint.setTextSize(TypedValue.applyDimension(unit, value, getResources().getDisplayMetrics()));
    }

    public void setTextColor(int color) {
        setTextColor(color, color);
    }

    public void setTextColor(int normal, int selected) {
        mNormalTextColor = normal;
        mSelectedTextColor = selected;
    }

    public void setText(String text) {
        mText = text;
        mTextWidth = TextUtils.isEmpty(mText) ? 0 : mPaint.measureText(mText);
        invalidate();
    }

    /**
     * 更新进度
     *
     * @param progress 0-1
     */
    public void setProgress(float progress) {
        mProgress = progress;
        Drawable drawable = getBackground();
        if (drawable instanceof IProgressDrawable) {
            ((IProgressDrawable) drawable).setProgress(progress);
        }
        postInvalidate();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mTextWidth = TextUtils.isEmpty(mText) ? 0 : mPaint.measureText(mText);
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        mTextHeight = fontMetrics.bottom - fontMetrics.descent - fontMetrics.ascent;
        if (mShadowBitmap != null) {
            if (getMeasuredWidth() != mShadowBitmap.getWidth() || getMeasuredHeight() != mShadowBitmap.getHeight()) {
                try {
                    mShadowBitmap.recycle();
                } catch (Throwable t) {
                }
                mShadowBitmap = null;
            }
        }
        if (mShadowBitmap == null && mNormalTextColor != mSelectedTextColor) {
            mShadowBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        }
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (TextUtils.isEmpty(mText)) {
            return;
        }
        float centerWidth = getWidth() / 2f;
        float centerHeight = getHeight() / 2f;
        float startTextX = centerWidth - mTextWidth / 2f;
        float startTextY = centerHeight + mTextHeight / 2f;
        if (mNormalTextColor == mSelectedTextColor) {
            mPaint.setColor(mNormalTextColor);
            canvas.drawText(mText, startTextX, startTextY, mPaint);
            return;
        }
        Canvas srcCanvas = new Canvas(mShadowBitmap);
        srcCanvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
        mPaint.setColor(mNormalTextColor);
        mPaint.setXfermode(null);
        srcCanvas.drawText(mText, startTextX, startTextY, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        mPaint.setColor(mSelectedTextColor);
        mProgressRectF.set(0, 0, getWidth() * mProgress, getHeight());
        srcCanvas.drawRect(mProgressRectF, mPaint);
        canvas.drawBitmap(mShadowBitmap, 0, 0, null);
    }

}